package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.pojo.Department;
import org.example.pojo.DepartmentEmployee;
import org.example.pojo.DepartmentRegister;
import org.example.pojo.DpartmentAdd;
import org.example.service.DepartmentService;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "department测试")
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    /**
     * 查询部门信息表
     * @return
     */
    @GetMapping("/showDepartment/{page}/{limit}")
    @ApiOperation("查询所有的部门")
    @ResponseBody
    public ResponseData showDepartmentByPage(@ApiParam(value = "当前页",required = true) @PathVariable int page, @ApiParam(value = "每一页的数据",required = true) @PathVariable int limit){

        PageHelper.startPage(page,limit);

        List<Department> departments = departmentService.showDepartmentAllByPage();

        PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);

        //以json传输
        ResponseData jsonData = ResponseData.getJsonData(departments,(int)departmentPageInfo.getTotal());
        if (departments!=null){
            return jsonData;
        }else {
            return ResponseData.error("没有部门数据",101);
        }
//        return jsonData;
    }

    /**
     * 通过用户名搜索查询
     * @param message
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/showDepartmentByName/{message}/{page}/{limit}")
    @ApiOperation("搜索关键字查询的部门")
    @ResponseBody
    public ResponseData showDepartmentsByName(@ApiParam(value = "输入的关键字",required = true) @PathVariable String message, @ApiParam("当前页") @PathVariable int page, @ApiParam("每一页的数据") @PathVariable int limit){
        PageHelper.startPage(page,limit);

        List<Department> departments = departmentService.showDepartmentsByName(message);

        PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);

        //以json传输
        ResponseData jsonData = ResponseData.getJsonData(departments,(int)departmentPageInfo.getTotal());
        if (departments!=null){
            return jsonData;
        }else {
            return ResponseData.error("没有该部门",101);
        }
//        return jsonData;
    }
    /**
     * 修改部门
     * @param
     * @return受影响的行数
     */
    @GetMapping("/selectDepartmentById/{dId}")
    @ApiOperation("通过id查询部门")
    @ResponseBody
    public ResponseData showDepartmentById(@ApiParam(value = "查询的ID",required = true)@PathVariable Integer dId){

        List<Department> department = departmentService.showDepartmentById(dId);

        PageInfo<Department> departmentPageInfo = new PageInfo<>(department);

        //以json传输
        ResponseData jsonData = ResponseData.getJsonData(department,(int)departmentPageInfo.getTotal());
        if (department!=null){
            return jsonData;
        }else {
            return ResponseData.error("没有该Id对应的部门",101);
        }
//        return jsonData;
//               Department department1=departmentService.showDepartmentById(dId);
//        PageInfo pageInfo=new PageInfo(department1);
//        //以json传输
//        ResponseData jsonData = ResponseData.getJsonData();

    }
    @PostMapping("/updateDepartment")
    @ApiOperation("修改部门")
    @ResponseBody
    public ResponseData updateDepartment(@ApiParam(value = "修改的数据有：部门名称，部门负责人， 负责人电话，负责人QQ，负责人邮箱，部门简介",required = true)DepartmentRegister departmentRegister){

        boolean flag=departmentService.updateDepartment(departmentRegister);

        if (flag){
            return ResponseData.getSccesss("修改成功",100);
        }else {
            return ResponseData.error("修改失败",101);
        }
    }

    /**
     * 通过id删除部门
     * @param dId
     * @return受影响的行数
     */
    @GetMapping("/deletDepartment/{dId}")
    @ResponseBody
    @ApiOperation("通过id删除部门")
    public ResponseData deletDepartment(@ApiParam( value = "删除部门id",required = true) @PathVariable Integer dId){

        int row=departmentService.deletDepartment(dId);

        if (row==1){
            return ResponseData.getSccesss("删除部门成功",100);
        }else {
            return ResponseData.error("删除部门失败",101);
        }
    }

    /**
     * 添加一级部门
     * @param dpartmentAdd
     * @return
     */
    @PostMapping("/addDepartmentOne")
    @ResponseBody
    @ApiOperation("添加部门")
    public ResponseData addDepartment(@ApiParam(value = "添加的数据有：上级部门id，部门名称，部门负责人，负责人电话，负责人QQ，负责人邮箱，部门简介",
            required = true)DpartmentAdd dpartmentAdd){
        System.out.println(dpartmentAdd);

        boolean flag=departmentService.addNewDepartment(dpartmentAdd);

        if (flag){
            return ResponseData.getSccesss("添加部门成功",100);
        }else {
            return ResponseData.error("添加部门失败",101);
        }
    }

    /**
     * 查询部门等级
     * @return
     */
    @GetMapping("/showDepartmentFirst/{dParentId}")
    @ApiOperation("查询部门等级")
    @ResponseBody
    public ResponseData showDepartmentFirst(@ApiParam(value = "查询部门的等级",required = true) @PathVariable Integer dParentId){
        List<String> firstDepartment = departmentService.showDepartmentFirst(dParentId);

        PageInfo<String> departmentPageInfo = new PageInfo<>(firstDepartment);

        //以json传输
        ResponseData jsonData = ResponseData.getJsonData(firstDepartment,(int)departmentPageInfo.getTotal());
        if (firstDepartment !=null){
            return jsonData;
        }else {
            return ResponseData.error("查询不到部门等级",101);
        }
//        return jsonData;
    }

    /**
     * 查询部门架构
     * @return
     */
    @GetMapping("/showDepartmentArch")
    @ApiOperation("查询部门架构")
    @ResponseBody
    public ResponseData showDepartmentArch(){
        List< Map<String,Object> > departments = departmentService.showDepartmentArch();

        PageInfo<Map<String,Object>> departmentPageInfo = new PageInfo<>(departments);

        //以json传输
        ResponseData jsonData = ResponseData.getJsonData(departments,(int)departmentPageInfo.getTotal());
        if (departments !=null){
            return jsonData;
        }else {
            return ResponseData.error("查询不到部门架构",101);
        }
//        return jsonData;
    }

    /**
     * 通部门查询部门员工
     * @param dName
     * @return
     */
    @GetMapping("/showDepartmentBydName/{dName}")
    @ResponseBody
    @ApiOperation("查询部门的员工姓名")
    public ResponseData showEmployeeNameByDepartment(@ApiParam(value = "部门名称",required = true) @PathVariable String dName){

        List<DepartmentEmployee> eName=departmentService.showEmployeeNameByDepartment(dName);
        PageInfo departmentPageInfo = new PageInfo<>();
        ResponseData jsonData = ResponseData.getJsonData(eName,(int)departmentPageInfo.getTotal());

        if (eName !=null){
            return jsonData;
        }else {
            return ResponseData.error("该部门没有员工信息",101);
        }


//        List<DepartmentEmployee> eName=departmentService.showEmployeeNameByDepartment(dName);
//        return eName;
    }

    /**
     * 通过id查询员工信息
     * @param eId
     * @return
     */
    @GetMapping("/showEmployeeById/{eId}")
    @ApiOperation("通过id查询员工信息")
    @ResponseBody
    public ResponseData showEmployeeById(@ApiParam(value = "员工Id",required = true) @PathVariable Integer eId){

        DepartmentEmployee departmentEmployee=departmentService.showEmployeeById(eId);
        PageInfo departmentPageInfo = new PageInfo<>();
        ResponseData jsonData = ResponseData.getJsonData(departmentEmployee,(int)departmentPageInfo.getTotal());

        if (departmentEmployee !=null){
            return jsonData;
        }else {
            return ResponseData.error("没有该员工信息",101);
        }

    }
}
